package com.zucc.controller;

import com.alibaba.fastjson.JSONArray;
import com.zucc.controller.req.*;
import com.zucc.controller.resp.MenuResp;
import com.zucc.service.MenuService;
import com.zucc.service.base.PageData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author : charspan
 * @Description :
 * @create : 2021-12-05 8:58 上午
 */
@RestController
@Slf4j
@RequestMapping("/v1/api/menus")
@Api(tags = "菜单管理")
public class MenuController {

    @Resource
    private MenuService menuService;

    @ApiOperation(value = "新增菜单")
    @PostMapping
    void add(@RequestBody @Validated MenuAddReq req) {
        menuService.add(req);
    }

    @ApiOperation(value = "修改菜单")
    @PutMapping
    void modify(@RequestBody @Validated MenuModifyReq req) {
        menuService.modify(req);
    }

    @ApiOperation(value = "删除菜单")
    @ApiImplicitParam(paramType = "path", name = "id", value = "菜单编号", dataType = "Long", example = "1", required = true)
    @DeleteMapping("/{id}")
    void deleteOne(@PathVariable Long id) {
        menuService.deleteOne(id);
    }

    @ApiOperation(value = "菜单分页")
    @PostMapping("/page")
    PageData<MenuResp> page(@RequestBody @Validated MenuSearchReq req) {
        return menuService.page(req);
    }

    @ApiOperation(value = "配置角色菜单权限(创建/修改)")
    @PostMapping("/role")
    void addRoleMenu(@RequestBody @Validated RoleMenuAddReq req) {
        menuService.addRoleMenu(req);
    }

    @ApiOperation(value = "根据角色类型获取角色菜单权限")
    @GetMapping("/role/{roleType}")
    List<MenuResp> getMenusByRoleType(@PathVariable Integer roleType) {
        return menuService.getMenusByRoleType(roleType);
    }

    @ApiOperation(value = "菜单树")
    @PostMapping("/tree")
    JSONArray getMenuTree(@RequestBody @Validated MenuSearchTreeReq req) {
        return menuService.getMenuTree(req);
    }
}
